import { Navigate, useRoutes } from 'react-router-dom'
import Login from '@/views/login'
// 导入所有router文件
const metaRouters = require.context('./modules', true, /\.js$/)

// 遍历所有router文件，将其导出的路由数组添加到routerArray数组中
export let routerArray = []

metaRouters.keys().forEach((key) => {
  // 获取每个文件导出的路由数组
  const routeModule = metaRouters(key)
  // 确保每个文件导出了一个数组
  if (Array.isArray(routeModule.default)) {
    routerArray = routerArray.concat(routeModule.default)
  } else {
    console.warn(`The file ${key} does not export a route array.`)
  }
})


export const rootRouter = [
  {
    path: '/',
    element: <Navigate to="/login" />,
  },
  {
    path: '/login',
    element: <Login />,
    meta: {
      requiresAuth: false,
      title: '登录页',
      key: 'login',
    },
  },
 	...routerArray,
  {
    path: '*',
    element: <Navigate to="/404" />,
  },
]
const Router = () => {
  const routes = useRoutes(rootRouter)
  return routes
}

export default Router
